var that,
  api = new(require('../../utils/api.js')),
  headimg = api.getimgurl(),
  staticimg = api.getstaticimgurl(),
  QQMapWX = require('../../utils/qqmap-wx-jssdk.js'),
qqmapsdk,
  util = new(require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scale:12,
    markers:[
      {
        id:6,
        latitude:37.4684,
        longitude:118.49892,
        title:'回收员1',
        iconPath:'../pic/can.png'
      }
    ]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
   wx.getLocation({
     success:res=>{
      that.setData({lat:res.latitude,lng:res.longitude,usertype:options.usertype},()=>{
        that.getaround()
      })
     }
   })
  },
  getaround(){
    let {usertype}=that.data
    if(usertype=='AGENT'){
      api.getmyusersaddresslist(res=>{
        if(res.data.issuccess==1){
          that.setmarkers(res.data.list)
        }
      })
    }else{
      let district = wx.getStorageSync('district');
      // let district=1259
      api.getnearbylist(district,res=>{
        if(res.data.issuccess==1){
          that.setmarkers(res.data.list)
        }
      })
    }
  },
  setmarkers(list){
    let markers=[]
    list.forEach((x,i)=>{
      let obj={
        id:x.tid,
        latitude:x.lat,
        longitude:x.lng,
        iconPath:'https://chinawzhs.cn/bk/static/img/markersicon.png',
        width:50,
        height:35,
        callout:{
          content:`姓名：${x.name}\n手机号：${x.phone}\n最近时间：${util.formatTimenosecend(new Date(x.updatedate))}`,
          borderRadius:10,
          padding:10,
          display:'BYCLICK',
      },
    }
    markers.push(obj)
    })
    that.setData({markers})
  },
  bindmarkertap(e){
   
    let{markers} = this.data,
   t =  markers.find(res=>{
      return res.id==e.markerId
    })
    console.log(t,'bindmarkertap');
    that.setData({
      lat:t.latitude,
      lng:t.longitude,
      scale:12
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})